package com.by_man.presenter.module_gestureLock;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.by_man.presenter.R;
import com.by_man.presenter.menu_activity.SettingActivity;
import com.by_man.repository.IpAddress;
import com.by_man.repository.PostStringRequest;
import com.by_man.repository.UserInfos;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by man on 2017-03-17.
 */
public class ModifyGestureLockActivity extends AppCompatActivity {
    private RequestQueue rQueue;
    private boolean isfirst = true;     // 是否是第一次输入
    private boolean isTwo = false;
    private GestureLockViewGroup mGestureLockViewGroup;
    private TextView tip;
    private int[] password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_gesturelock);
        rQueue = Volley.newRequestQueue(this);
        rQueue.start();
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, SettingActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(this, SettingActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        return super.onKeyDown(keyCode,event);
    }

    private void initView() {
        tip = (TextView) findViewById(R.id.tip);
        mGestureLockViewGroup = (GestureLockViewGroup) findViewById(R.id.id_gestureLockViewGroup);
        mGestureLockViewGroup.setOnGestureLockViewListener(new GestureLockViewGroup.OnGestureLockViewListener() {
            @Override
            public void onBlockSelected(int[] c) {
                if (isfirst) {
                    password = c;
                    mGestureLockViewGroup.setAnswer(c);     // 设置答案
                    tip.setText("请再次确认手势密码");
                    isfirst = false;
                } else {
                    if (!isTwo) {
                        isTwo = true;
                    }
                }
                mGestureLockViewGroup.resetOther();
            }

            @Override
            public void onGestureEvent(boolean matched) {
                    if (isTwo) {
                        if (matched) {      // 设置密码成功
                            String gesturepassword = "";
                            for (int i = 0; i < password.length; i++) {
                                gesturepassword += "" + password[i];
                            }
                            final String temp = gesturepassword;
                            HashMap<String, String> params = new HashMap<String, String>();
                            params.put("token", UserInfos.token);
                            params.put("mchoose", gesturepassword);
                            PostStringRequest stringRequest = new PostStringRequest(Request.Method.POST,
                                    IpAddress.Modify_GestureLock, params,
                                    new Response.Listener<String>() {
                                        @Override
                                        public void onResponse(String s) {
                                            try {
                                                JSONObject reponse = new JSONObject(s);
                                                boolean isUpdate = reponse.getBoolean("isUpdate");
                                                if (isUpdate) {
                                                    UserInfos.GestureLockAnswer = temp;
                                                    Toast.makeText(ModifyGestureLockActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                                                    Intent intent = new Intent(ModifyGestureLockActivity.this, SettingActivity.class);
                                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                                    startActivity(intent);
                                                } else {
                                                    Toast.makeText(ModifyGestureLockActivity.this, "设置失败，请重新输入", Toast.LENGTH_SHORT).show();
                                                    // 恢复
                                                    tip.setText("请输入新手势密码");
                                                    isfirst = true;
                                                    isTwo = false;
                                                }
                                            } catch (JSONException je) {
                                                je.printStackTrace();
                                            }
                                        }
                                    },
                                    new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError volleyError) {
                                            Log.e("ModifyGLError", volleyError.getMessage(), volleyError);
                                        }
                                    });
                            rQueue.add(stringRequest);
                        } else {
                            Toast.makeText(ModifyGestureLockActivity.this, "两次输入不一样，请重新输入！", Toast.LENGTH_SHORT).show();
                            isfirst = true;     // 恢复第一次输入
                            isTwo = false;
                            tip.setText("请输入新手势密码");
                        }
                    }
                mGestureLockViewGroup.reset();
            }

            @Override
            public void onUnmatchedExceedBoundary() {

            }
        });
    }
}
